﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatchResults.Services
{
    public abstract class ServiceBase<T> where T : class
    {
        protected abstract Uri GetUri(object param);
        protected abstract IEnumerable<T> ParseData(string data, object param);
        public abstract event EventHandler OnComplete;

        public virtual void GetMessages(Action<IEnumerable<T>> callback, object param)
        {
            WebClient client = GetWebClient();
            client.DownloadStringCompleted += (s, e) =>
                callback(e.Error == null ? ParseData(e.Result, param) : Enumerable.Empty<T>());
            client.DownloadStringAsync(GetUri(param));
        }

        private WebClient GetWebClient()
        {
            WebClient wc = new WebClient();
            AddWebClientHeaders(wc);
            return wc;
        }

        protected virtual void AddWebClientHeaders(WebClient wc)
        {
            wc.Headers["Accept"] = "*/*";
        }
    }
}
